;
/*==========================================
		I2C
===========================================*/
	.SECTION	.XBSS, BSS, XMEMORY

; I2C RECIEVE BUFS	
	RCV_BUF1:	.SPACE	2
	RCV_BUF2:	.SPACE	2
	RCV_BUF3:	.SPACE	2
	RCV_BUF4:	.SPACE	2
	
	RCV1_BUFH:	.SPACE	2
	RCV1_BUFL:	.SPACE	2


		.EQU	SLAVE_ID		,	0B0001011
		.EQU	COMMAND			,	0X09
		.EQU	I2C1BRG_VAL		,	390			;390 100K

	.INCLUDE	"p33FJ16GS502.INC"
	.INCLUDE 	"MACRO.INC"
	.GLOBAL		I2C_INITIAL
	.GLOBAL		RCV_BUFH,RCV_BUFL	; I2C RECIEVE BUF
	.GLOBAL		I2C_GET
	.TEXT


I2C_INITIAL:

	CLR		I2C1CON
	MOV		#0x1040,w0			; 1040
	MOV		w0,I2C1CON	
	CLR		I2C1MSK
	MOV		#I2C1BRG_VAL,w0		; Fcy=30MHz , Fscl=100KHz 
	MOV		w0,I2C1BRG
	BCLR	IFS1,#MI2C1IF		
	
	return
	
	
/*----------------------------------
	I2C CAMMAND SETS
-----------------------------------*/

I2C_START:
	BSET	I2C1CON,#SEN		; START
CHECK_I2C_START:
	BTSS	IFS1,#MI2C1IF
	GOTO	CHECK_I2C_START
	BCLR	IFS1,#MI2C1IF
	RETURN

;---------------------------
I2C_ADD_WRITE:
	RLNC	W0,W0
	AND		#0X00FE,W0
	MOV		W0,I2C1TRN
CHECK_I2C_ADD_WRITE:
	BTSS	IFS1,#MI2C1IF
	GOTO	CHECK_I2C_ADD_WRITE
	BCLR	IFS1,#MI2C1IF
	RETURN

;---------------------------
I2C_SEND_DATA:
	MOV		W0,I2C1TRN
CHECK_I2C_SEND_DATA:
	BTSS	IFS1,#MI2C1IF
	GOTO	CHECK_I2C_SEND_DATA
	BCLR	IFS1,#MI2C1IF
	RETURN

;---------------------------
I2C_ADD_READ:
	RLNC	W0,W0
	IOR		#0X0001,W0
	MOV		W0,I2C1TRN
CHECK_I2C_ADD_READ:
	BTSS	IFS1,#MI2C1IF
	GOTO	CHECK_I2C_ADD_READ
	BCLR	IFS1,#MI2C1IF
	RETURN

;---------------------------
I2C_RECEIVE_DATA:
	BSET	I2C1CON,#RCEN
CHECK_I2C_RECEIVE_DATA:
	BTSS	IFS1,#MI2C1IF
	GOTO	CHECK_I2C_RECEIVE_DATA
	BCLR	IFS1,#MI2C1IF
	RETURN

;---------------------------
I2C_STOP:
	BSET	I2C1CON,#PEN		; START
CHECK_I2C_STOP:
	BTSS	IFS1,#MI2C1IF
	GOTO	CHECK_I2C_STOP
	BCLR	IFS1,#MI2C1IF
	RETURN

;---------------------------
I2C_ACK:
	BCLR	I2C1CON,#ACKDT
	BSET	I2C1CON,#ACKEN		; START
CHECK_I2C_ACK:
	BTSS	IFS1,#MI2C1IF
	GOTO	CHECK_I2C_ACK
	BCLR	IFS1,#MI2C1IF
	RETURN

;JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
I2C_NACK:
	BSET	I2C1CON,#ACKDT
	BSET	I2C1CON,#ACKEN
CHECK_I2C_NACK:
	BTSS	IFS1,#MI2C1IF
	GOTO	CHECK_I2C_NACK
	BCLR	IFS1,#MI2C1IF
	RETURN
;---------------------------
I2C_RESTART:
	BSET	I2C1CON,#RSEN		; START
CHECK_I2C_RESTART:
	BTSS	IFS1,#MI2C1IF
	GOTO	CHECK_I2C_RESTART
	BCLR	IFS1,#MI2C1IF
	RETURN
	
/*----------------------------------
	1 CYCLE I2C COMMUNICATION	
-----------------------------------*/
I2C_GET:

	CALL	I2C_START

	MOV		#SLAVE_ID,W0
	CALL	I2C_ADD_WRITE

	MOV		#COMMAND,W0
	CALL	I2C_SEND_DATA

	CALL	I2C_RESTART

	MOV		#SLAVE_ID,W0
	CALL	I2C_ADD_READ

	CALL	I2C_RECEIVE_DATA
	MOVFF	I2C1RCV,RCV_BUF1	
	CALL	I2C_ACK
		
;JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ				
	CALL	I2C_RECEIVE_DATA
	MOVFF	I2C1RCV,RCV_BUF2	
	CALL	I2C_NACK
;JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

	CALL	I2C_STOP






	CALL	I2C_START

	MOV		#SLAVE_ID,W0
	CALL	I2C_ADD_WRITE

	MOV		#COMMAND,W0
	CALL	I2C_SEND_DATA

	CALL	I2C_RESTART

	MOV		#SLAVE_ID,W0
	CALL	I2C_ADD_READ

	CALL	I2C_RECEIVE_DATA
	MOVFF	I2C1RCV,RCV_BUF3	
	CALL	I2C_ACK
		
;JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ				
	CALL	I2C_RECEIVE_DATA
	MOVFF	I2C1RCV,RCV_BUF4	
	CALL	I2C_NACK
;JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

	CALL	I2C_STOP
	RETURN
	
.END